package controllers;

import play.mvc.Before;
import models.*;

public class Security extends Secure.Security {

	@Before
	static void checkAuthenticated() {
		if (!session.contains("username")) {
			flash.put("returnURL", request.url);
			login();
		}
	}

	public static void login() {
		flash.keep("returnURL");
		render();
	}

	public static boolean authenticate(String username, String password) {
		if (username.equals("guest"))
			return true;
		return User.find("byUserNameAndPassword", username, password).first() != null;
	}

	public static boolean isConnected() {
		return session.get("username") != null;
	}
}
